package reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test05 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> c1 = Class.forName("reflection.Student2");

        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得注解value的值
        Field f=c1.getDeclaredField("name");
        Fieldkuang annotation=f.getAnnotation(Fieldkuang.class);
        String value=annotation.columnName();
        System.out.println(value);
    }
}

@Tablekuang("db_student")
class Student2{
    @Fieldkuang(columnName = "id",type = "int",length = 10)
    private int age;
    @Fieldkuang(columnName = "name",type = "String",length =30)
    private String name;

    public Student2() {
    }

    public Student2(int age, String name) {
        this.age = age;
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return "Student2{age = " + age + ", name = " + name + "}";
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablekuang{
    String value();
}
//属性注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldkuang{
    String columnName();
    String type();
    int length();
}
